namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class OrganPositionLevelBLL
    {
        public void Add(OrganPositionLevel model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into OrganPositionLevel(");
            builder.Append("OrganName,LevelA,LevelB,LevelC");
            builder.Append(") values (");
            builder.Append("@OrganName,@LevelA,@LevelB,@LevelC");
            builder.Append(") ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrganName", model.OrganName), new SqlParameter("@LevelA", model.LevelA), new SqlParameter("@LevelB", model.LevelB), new SqlParameter("@LevelC", model.LevelC) };
            DBHelper.ExecuteSql(builder.ToString(), cmdParms);
        }

        public bool Delete(string OrganName)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrganPositionLevel ");
            builder.Append(" where OrganName=@OrganName ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrganName", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = OrganName;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public List<OrganPositionLevel> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM OrganPositionLevel ");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            OrganPositionLevel item = null;
            List<OrganPositionLevel> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<OrganPositionLevel>();
                foreach (DataRow row in table.Rows)
                {
                    item = new OrganPositionLevel {
                        OrganName = row["OrganName"].ToString()
                    };
                    if (row["LevelA"].ToString() != "")
                    {
                        item.LevelA = new int?(int.Parse(row["LevelA"].ToString()));
                    }
                    if (row["LevelB"].ToString() != "")
                    {
                        item.LevelB = new int?(int.Parse(row["LevelB"].ToString()));
                    }
                    if (row["LevelC"].ToString() != "")
                    {
                        item.LevelC = new int?(int.Parse(row["LevelC"].ToString()));
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public OrganPositionLevel GetModel(string OrganName)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select OrganName, LevelA, LevelB, LevelC  ");
            builder.Append("  from OrganPositionLevel ");
            builder.Append(" where OrganName=@OrganName ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrganName", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = OrganName;
            OrganPositionLevel level = new OrganPositionLevel();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                level.OrganName = row["OrganName"].ToString();
                if (row["LevelA"].ToString() != "")
                {
                    level.LevelA = new int?(int.Parse(row["LevelA"].ToString()));
                }
                if (row["LevelB"].ToString() != "")
                {
                    level.LevelB = new int?(int.Parse(row["LevelB"].ToString()));
                }
                if (row["LevelC"].ToString() != "")
                {
                    level.LevelC = new int?(int.Parse(row["LevelC"].ToString()));
                }
                return level;
            }
            return null;
        }

        public bool Update(OrganPositionLevel model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update OrganPositionLevel set ");
            builder.Append(" OrganName = @OrganName , ");
            builder.Append(" LevelA = @LevelA , ");
            builder.Append(" LevelB = @LevelB , ");
            builder.Append(" LevelC = @LevelC  ");
            builder.Append(" where OrganName=@OrganName  ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrganName", model.OrganName), new SqlParameter("@LevelA", model.LevelA), new SqlParameter("@LevelB", model.LevelB), new SqlParameter("@LevelC", model.LevelC) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

